package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Gareth Cao
 * @date 2022/6/17 11:56
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService wmMaterialService;

    /**
     * 上传图片到minio并在wm_material表中新增数据
     *
     * @param file
     * @return
     */
    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(@RequestParam("multipartFile") MultipartFile file) {
        ResponseResult responseResult = wmMaterialService.uploadPicture(file);
        return responseResult;
    }

    /**
     * 查询素材
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult findList(@RequestBody WmMaterialDto dto) {
        ResponseResult responseResult = wmMaterialService.findList(dto);
        return responseResult;
    }

    /**
     * 收藏
     */
    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer materialId) {
        wmMaterialService.collect(materialId);
        return ResponseResult.okResult("操作成功");
    }

    /**
     * 取消收藏
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult collectOrNor(@PathVariable("id") Integer materialId) {
        wmMaterialService.collectNor(materialId);
        return ResponseResult.okResult("操作成功");
    }

    @GetMapping("/del_picture/{id}")
    public ResponseResult deleteWithId(@PathVariable("id") Integer materialId) {
        wmMaterialService.deleteWithId(materialId);
        return ResponseResult.okResult("删除成功");
    }
}
